package org.example.controller;

import lombok.SneakyThrows;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.example.domain.Book;
import org.example.service.BookService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author TonySong
 * @date 2024/7/9 0009
 * @time 11:24
 */
@RestController
public class BookController {

    @Resource
    private BookService bookService;



    @GetMapping("/book/{id}")
    @Trace
    @Tags({
            @Tag(key="param",value = "arg[0]"),
            @Tag(key = "book",value = "returnedObj")
    })
    public Book findById(@PathVariable("id") Integer id){
        return this.bookService.getById(id);
    }

    @GetMapping("/stock")
    public Boolean stock(Integer id,Integer num){
         return this.bookService.stock(id,num);
    }

    /**
     * 图书列表
     * @return
     */
    @GetMapping("/books")
    @Trace
    @SneakyThrows
    @Tag(key = "findAll",value = "returnedObj")
    public List<Book> findAll(){
        //演示：模拟耗时操作：1秒钟
        TimeUnit.SECONDS.sleep(1);
        return this.bookService.list();
    }
}
